import datetime
import copy
from base import XMLBase, Reference, LinkType

DATATYPE_CHOICES = ('boolean', 'complex', 'datetime', 'double',
                    'float', 'int', 'long', 'rational', 'short', 'string')
CARDINALITY_CHOICES = ('0..1', '1', '0..*', '1..*')
CHARACTERIZATION_CHOICES = ('lower bound', 'upper bound',
                            'minimum', 'maximum',
                            'mean', 'standard deviation')

class Characterization(XMLBase):
    _node_name = 'Characterization'
    _attrib_map = {'Characterization':'value'}    

    def __init__(self, property=None, value=None,
                 quantity=None, units=None, ucd=None):
        
        self.property = property
        self.value = value
        self.quantity = quantity
        self.units = units
        self.ucd = ucd

    def to_tuple(self):

        atts = {}
        if self.property:
            atts['property'] = self.property
        if self.quantity:
            atts['quantity'] = self.quantity
        if self.units:
            atts['units'] = self.units
        if self.ucd:
            atts['ucd'] = self.ucd
        
        if atts:
            return ('Characterization', self.value, atts)
        else:
            return ('Characterization', self.value)

    def read(self, xml_str):
        XMLBase.read(self, xml_str)
        node = self._xmldoc.getElementsByTagName('Characterization')[0]
        property = node.getAttribute('property')
        if property:
            self.property = property
        quantity = node.getAttribute('quantity')
        if quantity:
            self.quantity = quantity
        units = node.getAttribute('units')
        if units:
            self.units = units
        ucd = node.getAttribute('ucd')
        if ucd:
            self.ucd = ucd

class Property(XMLBase):
    _node_name = 'Property'
    _attrib_map = {'Name': 'name',
                           'Description': 'description',
                           'Datatype':'datatype',
                           'Cardinality':'cardinality',
                           'UCD':'ucd'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference}}

    def __init__(self, name=None, description=None,
                 datatype='string', cardinality='1',
                 ucd=None,references=[]):
        
        self.name = name
        self.description = description
        self.datatype = datatype
        self.cardinality = cardinality
        self.ucd = ucd
        self.references = copy.copy(references)

class CharacterizableType(XMLBase):
    _attrib_map = {'Name': 'name',
                   'Description': 'description'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference},
                 'Property':{'attribute':'properties',
                              'class':Property}}

    def __init__(self, name=None, description=None,
                 keywords=[], properties=[],references=[]):

        self.name = name
        self.description = description
        self.keywords = copy.copy(keywords)
        self.properties = copy.copy(properties)
        self.references = copy.copy(references)

    def read(self, xml_str):
        XMLBase.read(self, xml_str)
        keyword_nodes = self._xmldoc.getElementsByTagName('Keyword')
        for keyword_node in keyword_nodes:
            self.keywords.append(keyword_node.childNodes[0].data)

class RepresentationLink(LinkType):
    _node_name = 'Representation'

class Representation(CharacterizableType):
    _node_name = 'Representation'

class TargetProcess(CharacterizableType):
    _node_name = 'TargetProcess'

class TargetObject(CharacterizableType):
    _node_name = 'TargetObject'
    _attrib_map = {'Name': 'name',
                   'Description': 'description',
                   'RealOBJID':'real_objid',
                   'Multiplicity':'multiplicity'}

    def __init__(self, name=None, description=None, 
                 real_objid=None, multiplicity=None,
                 keywords=[], properties=[],references=[]):

        CharacterizableType.__init__(self, name, description, 
                                     keywords, properties, references)
        self.real_objid = real_objid
        self.multiplicity = multiplicity

    def read(self, xml_str):
        CharacterizableType.read(self, xml_str)
        if self.multiplicity:
            self.multiplicity = int(self.multiplicity)
